﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Data;
using Motion.MotionPlatform.Tools;

namespace Motion.MotionPlatform
{
	/// <summary>
	/// 工具位置信息和结构管理类
	/// </summary>
	public class ToolsManager
	{
		public Dictionary<string, ToolsReset> ToolsCenter = new Dictionary<string, ToolsReset>();

		//加载工具表格，初始化工具参数
		public void Load()
		{
			if (File.Exists(MotionPlatformData.taskRootPath + MotionPlatformData.taskName + "/Tools.xls"))
			{
				ToolsCenter.Clear();
				ExcelOperator excelReader = new ExcelOperator();
				DataTable toolInitTable = excelReader.ExcelReader(MotionPlatformData.taskRootPath + MotionPlatformData.taskName +
					"/Tools.xls", "Tools", "A", "C");
				if (toolInitTable != null)
				{
					for (int i = 0; i < toolInitTable.Rows.Count; i++)
					{
						//Tools Name 处理
						string toolName = (string)toolInitTable.Rows[i][0].ToString();
						Transform toolTrans = null;
						try
						{
							toolTrans = GameObject.Find(toolName).transform;
						}
						catch
						{
							Debug.LogError("Tools.xls表格中" + toolName + "在场景中不存在，请检查！");
							continue;
						}
						//Tools Position 处理
						string tempPos = (string)toolInitTable.Rows[i][1].ToString();
						string[] posArray = tempPos.Split(',', '，');
						Vector3 tempPosVec = new Vector3(0f, 0f, 0f);
						try
						{
							tempPosVec.x = float.Parse(posArray[0]);
							tempPosVec.y = float.Parse(posArray[1]);
							tempPosVec.z = float.Parse(posArray[2]);
						}
						catch
						{
							Debug.LogError(MotionPlatformData.taskRootPath + MotionPlatformData.taskName +
								"/Tools.xls表中（" + (i + 2) + ", B）位置信息格式错误！");
						}
						//Tools EulerAngle 处理
						string tempEuler = (string)toolInitTable.Rows[i][2].ToString();
						string[] eulerArray = tempEuler.Split(',', '，');
						Vector3 tempEulerVec = new Vector3(0f, 0f, 0f);
						try
						{
							tempEulerVec.x = float.Parse(eulerArray[0]);
							tempEulerVec.y = float.Parse(eulerArray[1]);
							tempEulerVec.z = float.Parse(eulerArray[2]);
						}
						catch
						{
							Debug.LogError(MotionPlatformData.taskRootPath + MotionPlatformData.taskName +
								"/Tools.xls表中（" + (i + 2) + ", C）角度信息错误！");
						}
						//检测该物体是否已经包含
						if (ToolsCenter.ContainsKey(toolName))
						{
							ToolsCenter[toolName].Position = tempPosVec;
							ToolsCenter[toolName].EulerAngle = tempEulerVec;
						}
						else
						{
							ToolsReset toolsData = new ToolsReset();
							toolsData.Name = toolName;
							toolsData.Trans = toolTrans;
							toolsData.Position = tempPosVec;
							toolsData.EulerAngle = tempEulerVec;
							toolsData.Parent = toolTrans.parent;
							if (toolTrans.childCount > 0)
							{
								ChildLoad(toolTrans, toolsData);
							}
							if (ToolsCenter.ContainsKey(toolName))
							{
								Debug.LogError(toolName + "的子物体中有与其重名的物体，请用相关工具检查！");
							}
							else
							{
								ToolsCenter.Add(toolName, toolsData);
							}

						}
					}
				}
			}
			else
			{
				Debug.LogWarning("Tools.xls表格不存在，如果要使用工具返回功能，请创建该表格，否则运行过程中会报错！");
			}
		}

		//递归加载工具子物体信息
		private void ChildLoad(Transform parent_trans, ToolsReset parent_data)
		{
			foreach (Transform child in parent_trans)
			{
				ToolsReset toolsData = new ToolsReset();
				toolsData.Name = child.name;
				toolsData.Trans = child;
				toolsData.Position = child.position;
				toolsData.EulerAngle = child.eulerAngles;
				toolsData.Parent = child.parent;
				if (child.childCount > 0)
				{
					ChildLoad(child, toolsData);
				}
				parent_data.ChildList.Add(toolsData);
				if (ToolsCenter.ContainsKey(child.name))
				{
					Debug.LogError(child.name + "的子物体中有与其重名的物体，请用相关工具检查！");
				}
				else
				{
					ToolsCenter.Add(child.name, toolsData);
				}

			}
		}

		//工具位置设置
		public void PositionReset(string trans_name)
		{
			//Debug.Log("Parent: " + trans_name);
			ToolsCenter[trans_name].Trans.position = ToolsCenter[trans_name].Position;
			ToolsCenter[trans_name].Trans.eulerAngles = ToolsCenter[trans_name].EulerAngle;
			if (ToolsCenter[trans_name].ChildList.Count > 0)
			{
				for (int i = 0; i < ToolsCenter[trans_name].ChildList.Count; i++)
				{
					ChildReset(ToolsCenter[trans_name].ChildList[i]);
				}
			}
		}

		//工具子物体位置递归设置
		private void ChildReset(ToolsReset parent_data)
		{
			//Debug.Log(parent_data.Name + "---Reset");
			parent_data.Trans.position = parent_data.Position;
			parent_data.Trans.eulerAngles = parent_data.EulerAngle;
			if (parent_data.ChildList.Count > 0)
			{
				for (int i = 0; i < parent_data.ChildList.Count; i++)
				{
					ChildReset(parent_data.ChildList[i]);
				}
			}
		}

		//存储结构遍历，For Test
		public void DicTraverse()
		{
			List<string> keyList = new List<string>();
			foreach (string key in ToolsCenter.Keys)
			{
				keyList.Add(key);
			}
			foreach (string key in keyList)
			{
				Debug.Log("Tools: " + ToolsCenter[key].Name);
			}
		}
	}


	public class ToolsReset
	{
		public string Name { get; set; }

		public Transform Trans { get; set; }

		public Transform Parent { get; set; }

		public Vector3 Position { get; set; }

		public Vector3 EulerAngle { get; set; }

		public List<ToolsReset> ChildList;

		public ToolsReset()
		{
			Name = "";
			Trans = null;
			Parent = null;
			Position = Vector3.zero;
			EulerAngle = Vector3.zero;
			ChildList = new List<ToolsReset>();
		}
	}
}
